package com.example.thridhero.listviewtest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private List<Fruit> fruitList = new ArrayList<Fruit>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initFruits();
        FruitAdapter adapter =  new FruitAdapter(MainActivity.this,R.layout.fruit_item,fruitList);
        ListView listView = (ListView) findViewById(R.id.list_view);
        listView.setAdapter(adapter);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Fruit fruit = fruitList.get(position);
                Toast.makeText(MainActivity.this,fruit.getName(),Toast.LENGTH_SHORT).show();
            }
        });

    }
    private void initFruits(){
        for (int i = 0 ;i < 2; i++){
            Fruit apple = new Fruit("Apple",R.mipmap.apple_pic);
            fruitList.add(apple);

            Fruit banana = new Fruit("banana",R.mipmap.banana_pic);
            fruitList.add(banana);

            Fruit orange = new Fruit("orange",R.mipmap.orange_pic);
            fruitList.add(orange);

            Fruit watermelon = new Fruit("watermelon",R.mipmap.watermelon_pic);
            fruitList.add(watermelon);

            Fruit pear = new Fruit("pear",R.mipmap.pear_pic);
            fruitList.add(pear);

            Fruit grape = new Fruit("grape",R.mipmap.grape_pic);
            fruitList.add(grape);

            Fruit pineapple = new Fruit("pineapple",R.mipmap.pineapple_pic);
            fruitList.add(pineapple);

            Fruit strawberry = new Fruit("strawberry",R.mipmap.strawberry_pic);
            fruitList.add(strawberry);

            Fruit cherry = new Fruit("cherry",R.mipmap.cherry_pic);
            fruitList.add(cherry);

            Fruit mango = new Fruit("mango",R.mipmap.mango_pic);
            fruitList.add(mango);


        }
    }
}
